package com.rear.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.rear.entity.ImgHouseEntity;
import com.rear.service.ImgHouseService;
import com.rear.service.SysUserService;
import com.rear.utils.ImgBase64;
import com.rear.utils.PageUtils;
import com.rear.utils.R;


/**
 * 
 * 
 * @author October
 * @email chenzeming@9an-data.com
 * @date 2017-04-20 10:00:43
 */
@Controller
@RequestMapping("imghouse")
public class ImgHouseController {
	@Autowired
	private ImgHouseService imgHouseService;
	
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("/imghouse.html")
	public String list(){
		return "imghouse/imghouse.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("imghouse:list")
	public R list(Integer page, Integer limit,String cname,HttpServletRequest req){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		map.put("cname", cname);
		String username =req.getSession().getAttribute("username").toString();
		String channelid= sysUserService.getappid(username);
		map.put("channelid", channelid);
		//查询列表数据
		List<ImgHouseEntity> imgHouseList = imgHouseService.queryList(map);
		int total = imgHouseService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(imgHouseList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("imghouse:info")
	public R info(@PathVariable("id") Integer id){
		ImgHouseEntity imgHouse = imgHouseService.queryObject(id);
		
		return R.ok().put("imgHouse", imgHouse);
	}
	/**
	 * 赋予渠道编码
	 * @param imgHouse
	 * @param req
	 * @return
	 */
	public ImgHouseEntity setCid(ImgHouseEntity imgHouse,HttpServletRequest req){
		String username =req.getSession().getAttribute("username").toString();
		String channelid= sysUserService.getappid(username);
		imgHouse.setChannelid(channelid);
		imgHouse.setCreateuser(username);
		imgHouse.setUpdateuser(username);
		return imgHouse;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("imghouse:save")
	public R save(@RequestBody ImgHouseEntity imgHouse,HttpServletRequest req){
		//图片base64存储到服务器并返回路径
		String url=ImgBase64.GenerateImage(imgHouse.getImgBase64());
		imgHouse.setPicUrl(url);
		imgHouse=setCid(imgHouse, req);
		imgHouseService.save(imgHouse);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("imghouse:update")
	public R update(@RequestBody ImgHouseEntity imgHouse,HttpServletRequest req){
		
		//图片base64存储到服务器并返回路径
		if (imgHouse.getImgBase64()!=null&&imgHouse.getImgBase64()!="") {
			String url=ImgBase64.GenerateImage(imgHouse.getImgBase64());
			imgHouse.setPicUrl(url);
		}
		imgHouse=setCid(imgHouse, req);
		imgHouseService.update(imgHouse);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("imghouse:delete")
	public R delete(@RequestBody Integer[] ids){
		imgHouseService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
